import {
  Column,
  Entity,
  ManyToMany,
  Tree,
  TreeChildren,
  TreeParent,
} from 'typeorm';
import { Role } from '../../role/entities/role.entity';
import { CommonEntity } from '../../../../util/entity/common.entity';

@Entity('sys_menu')
@Tree('closure-table')
export class Menu extends CommonEntity {
  @Column({ length: '50', comment: '菜单名称' })
  name: string;

  // 当父组件是2目录的时候,子组件可以是1菜单,3按钮 4外链
  // 当父组件是1菜单的时候,子组件可以是3按钮,4外链
  // 父组件不能为3和4
  @Column({
    comment: '菜单类型 1:菜单；2:目录；3:按钮 4:外链',
  })
  type: number;

  // 当type为1,2,4的时候,这个选项必填
  @Column({ nullable: true, comment: '前端路由的path,就是router里面的path' })
  path: string;

  // 当type为1菜单时,必填 2-目录可填
  @Column({
    nullable: true,
    comment: '前端路由组件的路径,比如system-settings/system/user/index',
  })
  component: string;

  // 当type为3按钮或4的时候可填
  @Column({ nullable: true, comment: '权限,比如sys:user:add' })
  perm: string;

  @Column({ default: 1, comment: '排序,越小越靠前' })
  sort: number;

  @Column({ nullable: true, comment: 'icon' })
  icon: string;

  // 当type等于2目录的时候可填
  @Column({ nullable: true, comment: '重定向到哪里' })
  redirect: string;

  @TreeChildren()
  children: Menu[];

  // 会自动生成parentId
  @TreeParent()
  parent: Menu;

  @ManyToMany(() => Role, (role) => role.menus)
  roles: Role[];
}
